<?php

class Core_view 
{
	protected $_vars;
	
	protected $isAutoDisplay = true;
	
	protected $theme = 'default';
	
	protected $moduleName = 'index';
	
	protected $actionName = 'index';
	
	public function __construct($config = null)
	{
		$this->isAutoDisplay = isset($config['isAutoDisplay']) ? 
			$config['isAutoDisplay'] : Core_config::getConfig('VIEW_IS_AUTO_DISPLAY');
		$this->theme = isset($config['theme']) ? 
			$config['theme'] : Core_config::getConfig('VIEW_THEME');
	}
	
	public function assign($key, $value)
	{
		$this->_vars[$key] = $value;
	}
	
	public function display()
	{
		$themeFile = APP_ROOT.'/view/'.$this->theme.'/page/'.$this->moduleName.'/'.$this->actionName.Core_config::getConfig('VIEW_THEME_FILE_SUFFIX');
		if(file_exists($themeFile))
		{
			@include $themeFile;
		}
	}
	
	public function import($file, $folder = 'import')
	{
		if(!empty($file))
		{
			$themeFile = APP_ROOT.'/view/'.$this->theme.'/'.$folder.'/'.$file.Core_config::getConfig('VIEW_THEME_FILE_SUFFIX');
			if(file_exists($themeFile))
			{
				@include $themeFile;
			}
		}
	}
	
	public function setAutoDisplay($flag = true)
	{
		$this->isAutoDisplay = (bool)$flag;
	}
	
	public function __set($name, $value)
	{
		$allowSetVars = array('isAutoDisplay', 'theme', 'moduleName', 'actionName');
		if(in_array($name, $allowSetVars))
		{
			$this->$name = $value;
		}
	}
	
	public function __get($name)
	{
		if(isset($this->_vars[$name]))
		{
			return $this->_vars[$name];
		}
		else
		{
			return '';
		}
	}
	
	public function __destruct()
	{
		$this->display();
	}
}